package com.webproject.model.services;

import java.util.Collection;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.webproject.model.bean.ConsentBean;
import com.webproject.model.bean.PatientBean;
import com.webproject.model.interfaces.Clinic;

// TODO: Auto-generated Javadoc
/**
 * The Class HibernateClinicService.
 */
@Repository
@Transactional
public class HibernateClinicService implements Clinic {

	/** The session factory. */
	private SessionFactory sessionFactory;

	/**
	 * Instantiates a new hibernate clinic service.
	 * 
	 * @param sessionFactory
	 *            the session factory
	 */
	@Autowired
	public HibernateClinicService(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.webproject.model.interfaces.Clinic#addConsent(com.webproject.model
	 * .bean.ConsentBean)
	 */
	public void addConsent(ConsentBean consent) throws DataAccessException {
		// TODO Auto-generated method stub

	}

	/**
	 * Delete patient.
	 * 
	 * @param patientId
	 *            the patient id
	 * @throws DataAccessException
	 *             the data access exception
	 */
	public void deletePatient(int patientId) throws DataAccessException {
		// TODO Auto-generated method stub

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.webproject.model.interfaces.Clinic#findPatients(com.webproject.model
	 * .bean.PatientBean)
	 */
	@Transactional(readOnly = true)
	@SuppressWarnings("unchecked")
	public Collection<PatientBean> findPatients(PatientBean patientBean)
			throws DataAccessException {
		String where = "";

		if (patientBean.getName() != "") {
			where = where.concat(" patients.name like :name");
		}
		if (patientBean.getLastName() != "") {
			if (!where.isEmpty()) {
				where = where.concat(" and ");
			}
			where = where.concat(" patients.lastName like :lastName");
		}
		if (patientBean.getHistory() != "") {
			if (!where.isEmpty()) {
				where = where.concat(" and ");
			}
			where = where.concat(" patients.history like :history");
		}

		if (!where.equals("")) {
			where = " where " + where;
		}
		Query queryHibernate = sessionFactory.getCurrentSession().createQuery(
				"from PatientBean patients" + where);
		queryHibernate.setProperties(patientBean);
		Collection<?> list = queryHibernate.list();
		return (Collection<PatientBean>) list;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.webproject.model.interfaces.Clinic#loadConsent(int)
	 */
	public ConsentBean loadConsent(int id) throws DataAccessException {

		ConsentBean consent = new ConsentBean();
		consent.setPatient(loadPatient(id));
		// TODO Falta rellenar datos de los consentimientos
		return consent;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.webproject.model.interfaces.Clinic#loadPatient(int)
	 */
	public PatientBean loadPatient(int id) throws DataAccessException {
		PatientBean patient = null;
		if (id != 0) {
			patient = new PatientBean();
			patient.setId(id);
			String where = "";
			where = " where patients.id like :id";
			Query queryHibernate = sessionFactory.getCurrentSession()
					.createQuery("from PatientBean patients" + where);
			queryHibernate.setProperties(patient);
			patient = (PatientBean) queryHibernate.uniqueResult();
		}

		return patient;
	}

	/**
	 * Store patient.
	 * 
	 * @param patient
	 *            the patient
	 */
	public void storePatient(PatientBean patient) {
		// Note: Hibernate3's merge operation does not reassociate the object
		// with the current Hibernate Session. Instead, it will always copy the
		// state over to a registered representation of the entity. In case of a
		// new entity, it will register a copy as well, but will not update the
		// id of the passed-in object. To still update the ids of the original
		// objects too, we need to register Spring's
		// IdTransferringMergeEventListener on our SessionFactory.
		sessionFactory.getCurrentSession().merge(patient);
	}

}
